// pages/new-pages/my-employer-order/my-employer-order.js
const app = getApp();
import Toast from '../../../component/toast/toast';
import { formatAmount } from '../../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        positionId: 1,
        hasMore: true,
        pageSize: 10,
        pageIndex: -1,
        employeeList: []
    },
    //获取我的雇员列表
    getMyEmployee(data) {
        console.log("getMyEmployer" + JSON.stringify(data));
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        let that = this;
        wx.request({
            url: app.globalData.serverAddress.getMyEmployer,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function (res) {
                console.log("getMyEmployer response", res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        let count = res.data.BodyContent.TotalCount;
                        let flag = (that.data.pageIndex + 1) * that.data.pageSize < count;
                        let newList = that.data.employeeList.concat(res.data.BodyContent.List);
                        newList.forEach(function (item, index) {
                            newList[index]._amount = formatAmount(item.OrderAmount);
                        });
                        that.setEmployeeOrderStatus(newList);
                        that.setData({
                            hasMore: flag,
                            employeeList: newList
                        })
                    } else {
                        that.setData({
                            hasMore: false
                        });
                    }
                } else {
                    Toast("请求失败了，请检查网络连接！");
                }
            },
            fail: function (res) { console.log("getMyEmployer fail", res) },
            complete: function (res) { wx.hideLoading() },
        })
    },
    //根据订单状态设置页面的数据
    setEmployeeOrderStatus(list) {
        for (let i = 0; i < list.length; i++) {
            let orderStatus = list[i].OrderStatus;
            switch (orderStatus) {
                case 0:
                    //甲方未付款
                    list[i].statusIcon = '../../../assets/icon/icon_is_confirm.png';
                    list[i].statusTxt = '待客户确认';
                    break;
                case 1:
                    //甲方已付款
                    list[i].statusIcon = '../../../assets/icon/icon_is_confirm.png';
                    list[i].statusTxt = '执行中';
                    // list[i].statusBtn = '确认接单';
                    break;
                case 2:
                    //乙方接收
                    list[i].statusIcon = '../../../assets/icon/icon_complete.png';
                    list[i].statusTxt = '已完成';
                    break;
                case 3:
                    //乙方拒绝
                    list[i].statusIcon = '../../../assets/icon/icon_excuting.png';
                    list[i].statusTxt = '被解雇';
                    break;
            }
        }
    },
    //雇员列表的条件过滤
    getMyEmployeeFilter(isReach) {
        let data = {
            currentUserInfoId: app.userInfo.Id,
            orderType: this.data.positionId,
            pageIndex: 0,
            pageSize: this.data.pageSize
        }
        let that = this;
        if (isReach) {
            //下拉刷新
            if (!this.data.hasMore) {
                return;
            }
            let pageI = ++that.data.pageIndex;
            data.pageIndex = pageI;
            that.setData({
                pageIndex: pageI
            });
            this.getMyEmployee(data);
        } else {
            //非下拉刷新
            that.setData({
                pageIndex: 0
            })
            data.orderType = this.data.positionId;
            this.getMyEmployee(data);
        }
    },
    //结算方式
    clickPayWay(event) {
        let index = event.target.id;
        this.setData({
            positionId: index,
            pageIndex: -1,
            hasMore: true,
            employeeList: []
        });
        this.getMyEmployeeFilter(false);
    },
    clickListItem(e) {
        let index = e.currentTarget.dataset.index;
        let orderObj = this.data.employeeList[index];
        wx.navigateTo({
          url: '/pages/new-pages/my-employer-detail/my-employer-detail?orderObj=' + JSON.stringify(orderObj)
        })
    },
    toAgreement() {
        app.globalData.globalWebUrl = app.globalData.agreementUrl.employUrl;
        wx.navigateTo({
            url: '/pages/agreement-web/agreement-web?title=雇佣规则'
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getMyEmployeeFilter(false);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getMyEmployeeFilter(true);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})